﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Models;

namespace UI
{
    public partial class ProductIns : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.Label5.Visible = false;
                this.DropDownList1.DataSource = ProductBLL.Cha();
                this.DropDownList1.DataTextField = "ClassName";
                this.DropDownList1.DataValueField = "ClassID";
                this.DropDownList1.DataBind();
            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //上传图片文件并获取图片名称
            if (this.FileUpload1.HasFile)
            {
                string name = FileUpload1.FileName;
                string fix = name.Substring(name.LastIndexOf('.') + 1).ToLower();
                if (fix !="png" && fix !="jpg" && fix != "jpeg" && fix !="gif")
                {
                    this.Label5.Text = "上传的不是图片！";
                }
                else
                {
                    this.Label5.Visible = true;
                    FileUpload1.SaveAs(Server.MapPath(".") + "//Image//" + name);
                    Label5.Text = name;
                }
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            //添加商品操作
            Product p = new Product {
                PName = this.TextBox1.Text,
                PClass = this.DropDownList1.Text,
                PImg = this.Label5.Text,
                PPrice =Convert.ToInt32(this.TextBox3.Text)
            };
            if (ProductBLL.Inster(p))
            {
                Response.Redirect("PrpductUI.aspx");
            }
            else
            {
                Response.Write("添加失败！");
            }
        }
    }
}